<template>
  <div class="container mx-auto max-w-screen-xl mt-5" v-loading="loading">
    <el-card class="detail-card">
      <el-col class="header">
        <el-page-header icon="" title="">
          <template #icon>
            <el-icon color="#666" :size="20" @click="$router.go(-1)"> <Back /></el-icon>
          </template>
          <template #title>
            <span class="text" v-cursor-pointer @click="$router.go(-1)">返回</span>
          </template>
          <template #content>
            <div class="flex items-center">
              <span class="text-large font-800 mr-3 animate__animated animate__bounce name text">{{ categoryDetail.name }}</span>
            </div>
          </template>
          <el-row style="width: 100%" class="mt-4">
            <el-image class="cover" :src="categoryDetail.coverImgUrl" fit="cover" lazy>
              <template #placeholder>
                <Loading />
              </template>
            </el-image>
            <div style="flex: 1" class="info text-coolGray">
              <div class="font-800 text-base text-black dark:text-white">{{ categoryDetail.name }}</div>
              <div class="flex items-center mt-2">
                <el-image :src="categoryDetail.creator?.avatarUrl" style="width: 14px; height: 14px; border-radius: 50%" lazy class="mr-1"></el-image>
                <span class="mr-1">
                  {{ categoryDetail.creator?.nickname }}
                </span>
                <span v-for="itemTag in categoryDetail.distributeTags" :key="itemTag" class="pr-1 pl-1"> #{{ itemTag.tagName }} </span>
              </div>
              <p class="mt-4 text-sm description" v-text="categoryDetail.description" />
            </div>
          </el-row>
        </el-page-header>
        <el-col>
          <el-divider />
        </el-col>
      </el-col>
    </el-card>
    
    <el-card class="mt-3 music-list-card">
      <template #header>
        <div class="card-header">
          <span class="title">歌曲列表</span>
          <span class="count">共 {{ songList.length }} 首歌曲</span>
        </div>
      </template>
      <div class="list">
        <SongCard v-for="(item, index) in songList" :key="item.id" :data="item" @click="playMusic(item)">
          <div class="index mr-10">{{ index + 1 }}</div>
        </SongCard>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue"
import { useRoute } from "vue-router"
import SongCard from "./component/SongCard.vue"
import Loading from "@/components/Loading/index.vue"
import { getSongCategoryDetailAPI } from "@/api/web/music"

import { useMusicStore } from "@/store/modules/music"

// 音乐仓库
const musicStore = useMusicStore()

// 页面路由
const route = useRoute()

// 歌单详情
const categoryDetail = ref<any>({})

// 歌曲列表
const songList = ref<any[]>([])

// 获取歌单详情
const getCategoryetail = async () => {
  loading.value = true
  const res = await getSongCategoryDetailAPI(parseInt(route.query.id as string))
  console.log(res)
  if (res.code === 200) {
    categoryDetail.value = res.playlist
    songList.value = res.playlist?.tracks.slice(0, 20)
  }
  loading.value = false
}

// 点击歌曲播放音乐
const playMusic = (item: any) => {
  musicStore.playMusic(item)
  // console.log(e);
}

onMounted(() => {
  if (route.query.id) {
    getCategoryetail()
  }
})

// 加载
const loading = ref(false)
</script>

<style lang="scss" scoped>
// 歌单详情卡片样式
.detail-card {
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: none;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  
  ::v-deep(.el-card__body) {
    padding: 24px;
  }
  
  .header {
    .el-page-header {
      ::v-deep(.el-page-header__header) {
        .el-page-header__title {
          .text {
            color: #409eff;
            font-weight: 600;
            transition: color 0.3s ease;
            
            &:hover {
              color: #67c23a;
            }
          }
        }
        
        .el-page-header__content {
          .name {
            background: var(--gradient-music-success);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            font-size: 24px;
          }
        }
      }
    }
    
    .cover {
      width: 120px;
      height: 120px;
      border-radius: 16px;
      box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
      transition: all 0.3s ease;
      border: 3px solid #fff;
      
      &:hover {
        transform: scale(1.05);
        box-shadow: 0 12px 35px rgba(0, 0, 0, 0.2);
      }
    }
    
    .info {
      .font-800 {
        font-size: 20px;
        margin-bottom: 12px;
        color: #303133;
      }
      
      .flex.items-center.mt-2 {
        .mr-1 {
          color: #606266;
          font-size: 14px;
        }
        
        span {
          background: rgba(64, 158, 255, 0.1);
          color: #409eff;
          padding: 4px 8px;
          border-radius: 12px;
          font-size: 12px;
          margin-right: 8px;
          border: 1px solid rgba(64, 158, 255, 0.2);
          transition: all 0.3s ease;
          
          &:hover {
            background: rgba(64, 158, 255, 0.2);
            transform: translateY(-2px);
          }
        }
      }
      
      .description {
        color: #909399;
        line-height: 1.6;
        background: rgba(64, 158, 255, 0.05);
        padding: 16px;
        border-radius: 12px;
        border-left: 4px solid #409eff;
        margin-top: 16px;
      }
    }
    
    .el-divider {
      margin: 24px 0;
      
      ::v-deep(.el-divider__text) {
        background: transparent;
        color: #909399;
        font-size: 14px;
      }
    }
  }
}

.list {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  
  .index {
    width: 30px;
    height: 30px;
    margin-left: 10px;
    background-color: #409eff;
    text-align: center;
    line-height: 30px;
    color: #fff;
    font-size: 18px;
    border-radius: 50%;
  }
}

// 音乐列表卡片样式
.music-list-card {
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  border: none;
  overflow: hidden;
  
  ::v-deep(.el-card__header) {
    background: var(--gradient-music-primary);
    color: #fff;
    padding: 20px 24px;
    border-bottom: none;
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      
      .title {
        font-size: 20px;
        font-weight: 600;
      }
      
      .count {
        font-size: 14px;
        opacity: 0.9;
        background: rgba(255, 255, 255, 0.2);
        padding: 6px 12px;
        border-radius: 20px;
        backdrop-filter: blur(10px);
      }
    }
  }
  
  ::v-deep(.el-card__body) {
    padding: 24px;
    background: #fafbfc;
  }
}

.detail {
  background-size: cover;
  background-repeat: no-repeat;
  min-height: 100px;
}

::v-deep(.el-descriptions__body) {
  background: transparent;
}

.name {
  font-size: 16px;
}

.header {
  height: 100%;
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  max-height: 300px;

  .info {
    font-size: 12px;

    .description {
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
  }
}

.cover {
  width: 100px;
  height: 100px;
  margin-right: 20px;
  border: 5px solid #ccc;
  border-radius: 10px;
  margin-left: 10px;
}
</style>
